package com.xiaoshuidi.cloud.module.member.api.point;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.member.controller.admin.point.vo.MemberPointsChangeVo;
import com.xiaoshuidi.cloud.module.member.controller.app.point.vo.TenantPointsVo;
import com.xiaoshuidi.cloud.module.member.enums.point.PointRecordEnums;
import com.xiaoshuidi.cloud.module.member.service.point.MemberPointsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class PointsApiImpl implements PointsApi{

    @Resource
    MemberPointsService tenantPointsService;

    @Override
    public CommonResult<Boolean> plusTenantPoint(Long tenantUserId, Integer point, Integer recordType, String remark) {
        TenantPointsVo entity = tenantPointsService.getByTenantUserId(tenantUserId);
        MemberPointsChangeVo vo = new MemberPointsChangeVo();
        vo.setId(entity.getId());
        vo.setPoint(point);
        vo.setRemark(remark);
        PointRecordEnums enu = PointRecordEnums.getPointRecordEnumsByValue(recordType);
        tenantPointsService.changeTenantPoints(vo, enu);
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    public CommonResult<Boolean> minusTenantPoint(Long tenantUserId, Integer point, Integer recordType, String remark) {
        TenantPointsVo entity = tenantPointsService.getByTenantUserId(tenantUserId);
        MemberPointsChangeVo vo = new MemberPointsChangeVo();
        vo.setId(entity.getId());
        vo.setPoint(BigDecimal.valueOf(point).negate().intValue());
        vo.setRemark(remark);
        PointRecordEnums enu = PointRecordEnums.getPointRecordEnumsByValue(recordType);
        tenantPointsService.changeTenantPoints(vo, enu);
        return CommonResult.success(Boolean.TRUE);
    }
}
